#include"Star.h"
#define star_speed 0.25f
#define star_jump 0.05f
#define star_falling 0.02f
CStar::CStar(int _x, int _y, int _w, int _h)
{
	x=_x;
	y=_y;
	width=_w;
	height=_h;
	isactive=true;
	isshow=true;
	moveleft=false;
	bejump=false;
	beauto=false;
	istop=false;
	isfalling=false;
	isover=false;
	star=new CSprite(false,0,66,width,height,0,3,0);
}
CStar::~CStar()
{
	if(star!=NULL) delete star;
}
void CStar::Move(CStaticObjects *StaticObject, RECT screen)
{
	if(isshow==false) return;
	RECT starRect=star->getRECT(x,y);
	RECT objectRect;
	int kq=0;
	starRect.bottom=starRect.bottom-10;
	StaticObject->tree->Collision(StaticObject->tree->root,screen,starRect,&kq,&objectRect);
	if(kq!=0)
	{
		if(moveleft==false) moveleft=true;
		else moveleft=false;
	}
	starRect=star->getRECT(x,y);
	starRect.top=starRect.bottom-10;
	starRect.bottom+=10;
	StaticObject->tree->Collision(StaticObject->tree->root,screen,starRect,&kq,&objectRect);
	if(kq!=0)
	{
		bejump=true;
	}
	else bejump=false;
	
}
int CStar::CollisionWithMario(RECT marioRect)
{
	if(isover==true) return 0;
	RECT starRect=star->getRECT(x,y);
	RECT dest;
	if(IntersectRect(&dest,&starRect,&marioRect))
	{
		if(beauto==false)
		{
			isactive=true;
			isshow=true;
			return 0;
		}
		else
		{
			isshow=false;
			isover=true;
			return 1;
		}
	}
	return 0;
}
void CStar::Render(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9 image, int time, int xcamara)
{
	if(isactive==true)
	{
		y-=32;
		isactive=false;
		beauto=true;
	}
	if(beauto==true)
	{
		if(moveleft==true) vx=-star_speed;
			else vx=star_speed;
		if(bejump==true) vy=-5*star_jump;
		else vy=star_falling;
	//if(isfalling==true) vy+=star_falling;
		
		x+=vx*time;
		y+=vy*time*time;
	}
		if(isshow==true) 
	{
		star->Next();
		star->Render(sprite,image,x-xcamara,y);
	}
}